package com.wwdx.wechat.handler;

import com.wwdx.utils.ConstantHolder;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.joda.time.LocalDate;

import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author liguoliang
 */
public abstract class AbstractMenuHandler extends AbstractHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException{
        String openId = wxMessage.getFromUser(),eventKey = wxMessage.getEventKey(),clickKey = "card",clickKey2="placard";
        WxMpUser wxMpUser = wxMpService.getUserService().userInfo(openId);
        if(clickKey2.equalsIgnoreCase(eventKey)){
            String content="【蜜读听书馆】为您提供海量优质知识内容，有声书、小课、精品课，涵盖26大领域，今日邀请你来0元做馆长，操作简单，高额回报，每单可享受80%s佣金！\n\n" +
                    "<a href =\"%s"+"\">了解更多蜜读听书馆？</a>" + "\n\n" +
//                    "------------------------\n\n" +
//                    "<a href =\"%s"+"\">点击此处成为馆长</a>" + "\n\n" +
                    "↓↓分享下图你的专属海报，邀请好友一起学习~";
            WxMpKefuMessage message = new WxMpKefuMessage();
            message.setMsgType(WxConsts.KefuMsgType.TEXT);
            message.setToUser(openId);
            message.setContent(String.format(content,"%",wechatUrl+"promotionPage.html"));
            wxMpService.getKefuService().sendKefuMessage(message);
            //发送卡片
            wechatPublishService.sendCard(wxMpUser,wxMpService);
        }
        if (clickKey.equalsIgnoreCase(eventKey)){
            String date = new LocalDate().toString(),
                    key = String.format(ConstantHolder.USER_CARD_SET, openId),cardKey = String.format(ConstantHolder.USER_CARD_DAY_SET, date),
                    cardNewKey = String.format(ConstantHolder.USER_CARD_NEW_SET, date);
            //用户打卡总数
            setOperations.add(key,date + openId);
            Boolean member = setOperations.isMember(ConstantHolder.USER_CARD_TOTAL_SET, openId);
            //活跃
            setOperations.add(cardKey,openId);
            redisTemplate.expire(cardKey,30 * 60 * 60 , TimeUnit.SECONDS);
            //如果不存在，则为新打卡用户
            if (!member){
                //新增
                setOperations.add(cardNewKey,openId);
                redisTemplate.expire(cardNewKey,30 * 60 * 60 , TimeUnit.SECONDS);
                //打卡用户总数
                setOperations.add(ConstantHolder.USER_CARD_TOTAL_SET,openId);
            }
            //打卡次数
            int cardCount = setOperations.size(key).intValue() + 1;
            //听书书籍数
            int bookCount = new HashSet<>(listOperations.range(String.format(ConstantHolder.USER_DURATION_LIST,openId),0,-1)).size();
            String content = "早呀，很开心又能在清晨见到你，这是小蜜陪伴你的第"+cardCount+"个清晨。\n\n" +
                    "每天半小时，让读书成为一种习惯。\n" +
                    "\n" +
                    "<a href =\"%s"+"\">今日上新</a>\n";
            WxMpKefuMessage message = new WxMpKefuMessage();
            message.setMsgType(WxConsts.KefuMsgType.TEXT);
            message.setToUser(openId);
            message.setContent(String.format(content,wechatUrl+"auth.html"));
            wxMpService.getKefuService().sendKefuMessage(message);
            //发送卡片
            wechatPublishService.sendCard(wxMpUser,cardCount,bookCount,wxMpService);
        }
        return null;
    }
}